/*****************************************************************************/ /*!
* file  : systask.c
* creat : apleilx
* data  : 2017/11/05
*******************************************************************************/
#define _CS1237_TASK_MODULE_
#include "pincfg.h"
#include "systask.h"
#include "systimer.h"
#include "cs1237.h"
#include "sys_init.h"
#include "dataproc.h"
#include "filtern.h"

#define ADC_Bit 18 //ADC有效位数，带符号位 最高24位
#define CS_CON 0X10 //芯片地配置 内部REF 输出40HZ PGA=1 通道A

uint32_t cs1237_TimeCnt;
uint8_t ch_sel;

//当系统由 Power down 重新进入正常工作模式时，此时所有功能配置为 PowerDown 之前的状态，不需要进行功能配置。
void cs1237_obj_update(void);
void cs1237_ch_handle(uint8_t index);

/*****************************************************************************/ /*!
*
* @brief   cs1237 task.
*
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void cs1237task(char argument)
{
    cs1237_obj_update();
    
    if (bPulse_5ms)
    {
        cs1237_ch[0].TimeCnt++;
        cs1237_ch[1].TimeCnt++;
        
        ch_sel++;
        ch_sel&=0x01;
        
        cs1237_ch_handle(ch_sel);
    }
}

OBJ_APP_EXPORT(cs1237task);


void cs1237_ch_handle(uint8_t ch)
{
    if(ch > 1)
        return;
    
    switch (cs1237_ch[ch].st)
    {
        case 0:
        {
            cs1237_ch[ch].sck_out(0);
            cs1237_ch[ch].TimeCnt = 0;
            cs1237_ch[ch].st = 1;
            break;
        }
        case 1:
        {
            if (cs1237_ch[ch].TimeCnt > 2)
            {
                //config
                if (cs1237_cfg_set(&cs1237_ch[ch], CS_CON))
                {
                    cs1237_ch[ch].cfg_read = ~CS_CON;
                    cs1237_ch[ch].cfg_ext = CS_CON;
                    cs1237_ch[ch].st = 2;
                }
            }
            break;
        }
        case 2:
        {
            if (cs1237_cfg_read(&cs1237_ch[ch]))
            {
                if (cs1237_ch[ch].cfg_read == CS_CON)
                {
                    cs1237_ch[ch].st = 3;//config ok
                } else
                {
                    cs1237_ch[ch].st = 1;//config error
                }
                cs1237_ch[ch].cfg_read = ~CS_CON;
            }
            break;
        }
        case 3:
        {
            if (cs1237_cfg_read(&cs1237_ch[ch]))
            {
                if (cs1237_ch[ch].cfg_read == CS_CON)
                {
                    cs1237_ch[ch].st = 4;//config ok
                } else
                {
                    cs1237_ch[ch].st = 1;//config error
                }
            }
            break;
        }
        case 4:
        {
            // read
            if(cs1237_ad_read(&cs1237_ch[ch]))
            {
                cs_ad_res.list_index[ch]&=0x3F;
                cs_ad_res.list[ch][cs_ad_res.list_index[ch]] = cs1237_ch[ch].ad_val;
                cs_ad_res.list_index[ch]++;
                
                cs_ad_res.val[ch] = i32_average_filter(cs_ad_res.list[ch],64);
                
                cs_ad_res.ready[ch] = 1;
            }
            break;
        }
        default:
        {
            cs1237_ch[ch].st = 0;
            break;
        }
    }
}

// pin interface
void sck1_out(uint8_t out)
{
    if (out)
    {
        SCK1_Out(1);
    }
    else
    {
        SCK1_Out(0);
    }
}
void dat1_out(uint8_t out)
{
    if (out)
    {
        DIO1_Out(1);
    }
    else
    {
        DIO1_Out(0);
    }
}
void dat1_dir(uint8_t out_or_in)
{
    if (out_or_in)
    {	  
        GPIO_CTL0(GPIOA) &= ~(0x0FUL << 4*4);
        GPIO_CTL0(GPIOA) |= 3UL << 4*4;
    }
    else
    {
        GPIO_CTL0(GPIOA) &= ~(0x0FUL << 4*4);
        GPIO_CTL0(GPIOA) |= 8UL << 4*4; 
        DIO1_Out(1);
    }
}
uint8_t dat1_in(void) { return DIO1_St(); }

void sck2_out(uint8_t out)
{
    if (out)
    {
        SCK2_Out(1);
    }
    else
    {
        SCK2_Out(0);
    }
}
void dat2_out(uint8_t out)
{
    if (out)
    {
        DIO2_Out(1);
    }
    else
    {
        DIO2_Out(0);
    }
}
void dat2_dir(uint8_t out_or_in)
{
    if (out_or_in)
    {	  
        GPIO_CTL0(GPIOA) &= ~(0x0FUL << 4*6);
        GPIO_CTL0(GPIOA) |= 3UL << 4*6;
    }
    else
    {
        GPIO_CTL0(GPIOA) &= ~(0x0FUL << 4*6);
        GPIO_CTL0(GPIOA) |= 8UL << 4*6; 
        DIO2_Out(1);
    }
}
uint8_t dat2_in(void) { return DIO2_St(); }


void cs_delay_0_5us(uint8_t us)
{
    NOP20();NOP20();
    NOP20();
}

/*****************************************************************************/ /*!
*
* @brief   cs1237 task.
*
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void cs1237Init(char argument)
{
    gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_4);
    gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5);
    gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
    gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_7);
    cs1237_obj_update();
    
}
OBJ_INIT_APP_EXPORT(cs1237Init);

// object update
void cs1237_obj_update(void)
{
    cs1237_ch[0].sck_out = sck1_out;
    cs1237_ch[0].dat_out = dat1_out;
    cs1237_ch[0].dat_in = dat1_in;
    cs1237_ch[0].dat_dir = dat1_dir;

    cs1237_ch[1].sck_out = sck2_out;
    cs1237_ch[1].dat_out = dat2_out;
    cs1237_ch[1].dat_in = dat2_in;
    cs1237_ch[1].dat_dir = dat2_dir;

    
    cs1237_ch[0].bits = ADC_Bit;
    cs1237_ch[1].bits = ADC_Bit;
    cs1237_ch[0].delay_0_5us = cs_delay_0_5us;
    cs1237_ch[1].delay_0_5us = cs_delay_0_5us;
}
